/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cl.osera.model

import net.liftweb._
import mapper._
import http._
import SHtml._
import common._
import util._

//import scala.math._

import xml.{NodeSeq,Text}

import java.util.Date
import java.text.DateFormat
// import java.util.Calendar
import java.text.SimpleDateFormat
import java.text.DecimalFormat;
import java.text.ParseException


class ToDo extends LongKeyedMapper[ToDo] with IdPK {
  def getSingleton = ToDo

  object done     extends MappedBoolean( this )
  object owner extends MappedLongForeignKey(this, User)

  object dateOf extends MappedDateTime(this) {
    override def defaultValue = new java.util.Date
//    override def defaultValue = Calendar.getInstance().getTime()

//    final val dateFormat =
//      DateFormat.getDateInstance(DateFormat.SHORT)
//    override def asHtml = Text(dateFormat.format(is))

    final val myDateFormat =
      new SimpleDateFormat("dd-MM-yyyy")

    override def format(d: Date): String =
      myDateFormat.format(d)

    override def parse(s: String): Box[Date] =
      try {
        Box(myDateFormat.parse(s))
      } catch {
        case ex : java.text.ParseException => null
      }

  }

  def myToString( m: Int ): String = {
    val form=new java.text.DecimalFormat("00")
    if (m>=0) ((m-(m%60))/60) + ":" + form.format(m%60)
      else "-" +myToString(-m)
  }

  def myToInt( m: String ): Int = {
    val m2=m.trim
    if (m2.substring(0,1) == '-') (-1 * myToInt(m2.substring(1)))
    else {
      val pos=m2.lastIndexOf(':')
      if (pos < 0) m2.toInt
      else m2.substring(0,pos).toInt *60 + m2.substring(pos+1).toInt
    }
  }

  def myDate2Mins( d: Date ): Int = 
    myToInt( new java.text.SimpleDateFormat("H:m").format(d) )

  object inicio extends MappedInt(this) {
    override def defaultValue = myDate2Mins( new java.util.Date )

    override def toString: String = myToString(is)

  }

  object duration extends MappedInt(this) {
    override def defaultValue = 5

    override def toString: String = myToString(is)
  }


//   object priority extends MappedInt( this ) {
//     override def defaultValue = 5
//   }
   object priority extends MappedInt(this) {
         override def defaultValue = 5
   
         override def validations = validPriority _ :: super.validations
   
         def validPriority(in: Int) : List [FieldError] =
                 if (in > 0 && in <= 10) Nil
                 else List(FieldError(this, <b>Priority must be 1-10</b>) )
   
         override def _toForm = Full( select (ToDo.priorityList,
                 Full(is.toString),
                 f => set(f.toInt) ) )
   }

   // object desc extends MappedPoliteString ( this , 128)
   object desc extends MappedPoliteString(this, 128) {
      override def validations =
         valMinLen(3, "Description must be 3 characters") _ :: super.validations
   }  
}

//object ToDo extends ToDo with LongKeyedMetaMapper [ToDo ]
object ToDo extends ToDo with LongKeyedMetaMapper [ToDo ] {
	lazy val priorityList = (1 to 10).
	map( v => (v.toString, v.toString) )
}

